// pages/recharge/recharge.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    charge:'',
    isCanSubmit:false,
    userInfo: '',
    page:1,
    detail:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      page: options.page
    })
    if (options.page=='1'){
      wx.setNavigationBarTitle({
        title: '付款',
      })
      this.getDetail(options.id);
    }
  },
  getDetail: function (id) {
    var obj = { '1': '车找人', '2': '人找车', '3': '货找车', '4': '车找货' };
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'freeRidePay/page', //仅为示例，并非真实的接口地址
      data: {
        guestId: this.data.userInfo.id,
        status: 1,
        id:id
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            detail: res.data.data.pp[0]
          });
        }
      }
    })
  },
  submitPay:function(){
    if(this.data.page=='1'){
      this.pulish();
    }
  },
  pulish:function(){
    var that=this;
    var item=this.data.detail;
    wx.request({
      url: app.globalData.httpApiCar + 'freeRidePay/surplusTravelExpensesPayment', //仅为示例，并非真实的接口地址
      data: {
        openId: app.globalData.openId,
        type: 1,
        backupA: item.backupF,
        backupB: item.id,
        backupC: item.type,
        backupD: 4,
        adds: 2,
        userId: this.data.userInfo.id,
        body: '顺风车支付剩余路费',
        money: this.data.charge
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          that.requestPayment(res.data.data);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.error,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
              }
            }
          })
        }
      }
    })
  },
  requestPayment: function (obj) {
    var that = this;
    wx.requestPayment({
      'timeStamp': obj.timeStamp,
      'nonceStr': obj.nonceStr,
      'package': obj.package,
      'signType': obj.signType,
      'paySign': obj.paySign,
      'success': function (res) {
        wx.navigateBack({
          delta:1
        })
      },
      'fail': function (res) {
        wx.showModal({
          title: '提示',
          content: '支付失败',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },
  changeInput: function (e){
    this.setData({
      charge: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    var canSubmit = true;
    if (this.data.charge == '') {
      canSubmit = false;
    }
    if (this.data.charge <= 0) {
      canSubmit = false;
    }
    this.setData({
      isCanSubmit: canSubmit
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})